package com.wjj.sixinone.service.impl;

import com.wjj.sixinone.common.pojo.excel.LogExcel;
import com.wjj.sixinone.common.pojo.excel.TpServerLogExcel;
import com.wjj.sixinone.common.pojo.log.ExceptionLogs;
import com.wjj.sixinone.common.pojo.log.TPServerLogs;
import com.wjj.sixinone.common.pojo.vo.LogVO;
import com.wjj.sixinone.common.util.Result;
import com.wjj.sixinone.handler.ExcelExportHandle;
import com.wjj.sixinone.service.LogService;
import jakarta.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LogServiceImpl implements LogService {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private ExcelExportHandle exceptionHandler;
    @Override
    public Result findExceptionLogPaginated(LogVO logVO) {
        String ip = logVO.getIp();
        String username = logVO.getUsername();
        Integer code = logVO.getCode();

        // 创建查询条件
        Query query = new Query();
        Query countQuery = new Query();

        // 添加排序规则
        query.with(Sort.by(Sort.Direction.DESC, "createTime"));

        // 如果参数不为空，则添加相应的查询条件
        if (ip != null && !ip.isEmpty()) {
            query.addCriteria(Criteria.where("ip").is(ip));
            countQuery.addCriteria(Criteria.where("ip").is(ip));
        }
        if (username != null && !username.isEmpty()) {
            query.addCriteria(Criteria.where("username").is(username));
            countQuery.addCriteria(Criteria.where("username").is(username));
        }
        if (code != null) {
            query.addCriteria(Criteria.where("code").is(code));
            countQuery.addCriteria(Criteria.where("code").is(code));
        }

        // 计算总记录数
        long total = mongoTemplate.count(countQuery, "exception_logs");

        // 计算skip的偏移量
        int skip = (logVO.getPage()-1) * logVO.getLimit();

        // 应用分页
        query.skip(skip).limit(logVO.getLimit());

        // 执行查询
        List<ExceptionLogs> results = mongoTemplate.find(query, ExceptionLogs.class, "exception_logs");

        // 返回结果
        return Result.ok().put("data", results).put("count", total);

    }

    @Override
    public void uploadExcel(HttpServletResponse response) {
        List<LogExcel>logExcels=new ArrayList<>();

          // 查询所有异常日志
        List<ExceptionLogs> logs = mongoTemplate.findAll(ExceptionLogs.class, "exception_logs");
                for(ExceptionLogs exceptionLogs1 : logs){
                    LogExcel logExcel=new LogExcel();
                    BeanUtils.copyProperties(exceptionLogs1,logExcel);
                    logExcels.add(logExcel);
                }
                BeanUtils.copyProperties(logs, logExcels);
                exceptionHandler.export(response, "系统异常信息表",logExcels, LogExcel.class);

    }

    @Override
    public Result tpserverLog(LogVO logVO) {
        String ip = logVO.getIp();
        String username = logVO.getUsername();
        Integer code = logVO.getCode();

        // 创建查询条件
        Query query = new Query();
        Query countQuery = new Query();

        // 添加排序规则
        query.with(Sort.by(Sort.Direction.DESC, "createTime"));

        // 如果参数不为空，则添加相应的查询条件
        if (ip != null && !ip.isEmpty()) {
            query.addCriteria(Criteria.where("ip").is(ip));
            countQuery.addCriteria(Criteria.where("ip").is(ip));
        }
        if (username != null && !username.isEmpty()) {
            query.addCriteria(Criteria.where("username").is(username));
            countQuery.addCriteria(Criteria.where("username").is(username));
        }
        if (code != null) {
            query.addCriteria(Criteria.where("errorCode").is(code));
            countQuery.addCriteria(Criteria.where("errorCode").is(code));
        }

        // 计算总记录数
        long total = mongoTemplate.count(countQuery, "tpServer_logs");

        // 计算skip的偏移量
        int skip = (logVO.getPage()-1) * logVO.getLimit();

        // 应用分页
        query.skip(skip).limit(logVO.getLimit());

        // 执行查询
        List<TPServerLogs> results = mongoTemplate.find(query, TPServerLogs.class, "tpServer_logs");

        // 返回结果
        return Result.ok().put("data", results).put("count", total);
    }

    @Override
    public void uploadTPserverExcel(HttpServletResponse response) {

        List<TpServerLogExcel>logExcels=new ArrayList<>();

        // 查询所有异常日志
        List<TPServerLogs> logs = mongoTemplate.findAll(TPServerLogs.class, "tpServer_logs");
        for(TPServerLogs exceptionLogs1 : logs){
            TpServerLogExcel tpServerLogExcel=new TpServerLogExcel();
            BeanUtils.copyProperties(exceptionLogs1,tpServerLogExcel);
            logExcels.add(tpServerLogExcel);
        }
        BeanUtils.copyProperties(logs, logExcels);
        exceptionHandler.export(response, "第三方服务调用记录表",logExcels, TpServerLogExcel.class);

    }
}
